import router, {resetRouter} from '@/router/index'
import store from "@/store";
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {cleanCache,} from '@/utils/tools'
const whiteList = ["/login", '/404']
router.beforeEach(async (to, from, next) => {
    NProgress.start()
    if (whiteList.indexOf(to.path) !== -1) {
        next()
    } else {
        if (sessionStorage.getItem('token')) {
            if (store.getters.getMenu && store.getters.getMenu.length === 0) {
                //  获取当前角色所有权限菜单和按钮
                let menuPermissions = await store.dispatch('getMenuList')
                sessionStorage.setItem('menuList', JSON.stringify(menuPermissions))
                await store.dispatch('convertRoutes', menuPermissions)
                next({...to, replace: true})
            } else {
                next()
            }
        } else {
            cleanCache()
            next({
                path: '/login'
            })
        }
    }

})
router.afterEach((to) => {
    /**
     * 三级路由无法缓存问题
     * 删除父级视图
     */
    if (to.matched && to.matched.length > 2) {
        for (let i = 0; i < to.matched.length; i++) {
            const element = to.matched[i]
            if (element.components.default.name === 'black') {
                to.matched.splice(i, 1)
            }
        }
    }
    NProgress.done()

})
